package com.zzptc.zzsgz.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.zzsgz.bdsjws.com.Constants;
import com.zzptc.zzsgz.bdsjws.db.BBXDao;
import com.zzptc.zzsgz.bdsjws.db.DaoMaster;
import com.zzptc.zzsgz.bdsjws.db.DaoSession;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;
import com.zzptc.zzsgz.bdsjws.vo.BBX;

import org.greenrobot.greendao.database.Database;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

import static com.zzptc.zzsgz.bdsjws.com.Constants.DATABASE_NAME;

/**
 * Created by zzsgz on 2017/4/20.
 */

public class MyApplication extends Application {
    //功能：1、提供全局的上下文 2、初始化KLOG
    static Context context;
    static DaoSession daoSession;
    static DaoSession mobileSession;
//真正的入口方法
    @Override
    public void onCreate() {
        super.onCreate();
        context=getApplicationContext();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        initClient();
        initDatebase();
        initDatebase1();

        new Thread(new Runnable() {
            @Override
            public void run() {
                initDatatoTable();
                //外部数据库拷贝到手机存储器中
                try {
                    MyUtil.copyFileToMobile(context,"mobile.db","mobile.db");
                    MyUtil.getAllContacts();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public void initClient(){

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);

    }
    public static Context getContext(){

        return context;
    }
    //初始化
    public void initDatebase(){
        DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,DATABASE_NAME);
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
         daoSession=daoMaster.newSession();
    }
    public static DaoSession getDaoSession(){
        return daoSession;
    }
    public void initDatebase1(){
        DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,"mobile.db");
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        mobileSession=daoMaster.newSession();
    }
    public static DaoSession getMobileSession(){
        return mobileSession;
    }


    //将数据新增到表 要对表进行增删改查 必须要获取BBXDao对象 只能操作一次 用共享首选项
   public void initDatatoTable (){
       DaoSession daoSession=getDaoSession();
      BBXDao bbxDao= daoSession.getBBXDao();

       /*try {
           MyUtil.copyFileToMobile(context,"mobile.db");
       } catch (IOException e) {
           e.printStackTrace();
       }*/

       SharedPreferences sharedPreferences= MyUtil.getSharedPre();
      boolean b= sharedPreferences.getBoolean("inserttable",false);
       //控制初始化一次
       if (!b){
       //数据源在哪 strings.xml中
       // 读取 string.xml
       String[] names=getResources().getStringArray(R.array.bbx_name);
       String[] icons=getResources().getStringArray(R.array.bbx_icon);
       for (int i=0;i<names.length;i++) {
           BBX bbx = new BBX(null, names[i], icons[i]);
           try {
               long m = bbxDao.insert(bbx);
               if (m > 0) {
                  // Toast.makeText(context, m+"新增成功", Toast.LENGTH_SHORT).show();
                   KLog.i("新增成功");
               } else {
                   //Toast.makeText(context,m+ "新增失败", Toast.LENGTH_SHORT).show();
                   KLog.i("新增失败");
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
           int len=bbxDao.loadAll().size();
           KLog.i("***********新增"+len+"记录*********");
           for (BBX bbx:bbxDao.loadAll()){
               KLog.i(bbx.getName()+"=========="+bbx.getIcon());
           }
           //新增完毕，将共享首选项的值该为true
          SharedPreferences.Editor editor= sharedPreferences.edit();
           editor.putBoolean("inserttable",true);
           editor.commit();
       }
   }

}
